from windoh.control import Control
from windoh.controls.label import Label
from windoh.controls.button import Button

class Window(Control):
    class WindowLabel(Label):
        def on_mouse_press(self, x, y, b, m):
            cs = self._computed_style
            self._parent.dispatch_event('on_window_start_move', cs['left'] + x, cs['bottom'] + y)
            self._parent.bring_to_front()
            return True

    class WindowResizeHandle(Control):
        def __init__(self, corner, **kwargs):
            self.corner = corner
            Control.__init__(self, **kwargs)
        
        def on_mouse_press(self, x, y, b, m):
            cs = self._computed_style
            self._parent.dispatch_event('on_window_start_resize', 
                self.corner, cs['left'] + x, cs['bottom'] + y)
            self._parent.bring_to_front()
            return True

    def __init__(self, label=None, **kwargs):
        Control.__init__(self, **kwargs)

        
        self._title = self.WindowLabel(label, classname="window_title", 
            left=1, top=1, right=1, height=20)
        self._children.append(self._title)
        
        self._client_area = Control(classname="window_client_area", 
            left=1, right=1, top=21, bottom=1)
        self._children.append(self._client_area)
        self._children_proxy = self._client_area._children
        
        sz = 15
        dh = self.WindowResizeHandle(0, name="0", classname="window_resize_handle", 
            left=0, bottom=0, width=sz, height=sz, cursor="size_down_left")
        self._children.append(dh)

        dh = self.WindowResizeHandle(1, name="1", classname="window_resize_handle", 
            right=0, bottom=0, width=sz, height=sz, cursor="size_down_right")
        self._children.append(dh)

        dh = self.WindowResizeHandle(2, name="2", classname="window_resize_handle", 
            right=0, top=0, width=sz, height=sz, cursor="size_up_right")
        self._children.append(dh)

        dh = self.WindowResizeHandle(3, name="3", classname="window_resize_handle", 
            left=0, top=0, width=sz, height=sz, cursor="size_up_left")
        self._children.append(dh)

    def on_window_start_move(self, offx, offy):
        self._drag_offset = (offx, offy)
        self._drag_mode = 'move'
        self._drag_corner = None
        self.start_drag()

    def on_window_start_resize(self, corner, offx, offy):
        self._drag_offset = (offx, offy)
        self._drag_corner = corner
        self._drag_mode = 'resize'
        self.start_drag()

    def on_drag(self, x, y, dx, dy, b, m):
        x = x - self._parent._computed_style['screen_x']
        y = y - self._parent._computed_style['screen_y']
        if self._drag_corner is None:
            self.dispatch_event('on_window_move', x, y)
        else:
            self.dispatch_event('on_window_resize', self._drag_corner, x, y)

    def on_window_move(self, x, y):
        x = x - self._drag_offset[0]
        y = y - self._drag_offset[1]

        s = self.style
        cs = self._computed_style
        pcs = self._parent._computed_style
        
        # need to generalise this for resize and other funcs
        if self.style['left'] is not None:
            has_top = (s.has_key('top') and s['top'] is not None)
            has_bottom = (s.has_key('bottom') and s['bottom'] is not None)

            if has_top and has_bottom:
                self.style.update(dict(
                    left=x,
                    top=pcs['height']-y-cs['height'],
                    bottom=y,
                ))
            elif has_top:
                self.style.update(dict(
                    left=x,
                    top=pcs['height']-y-cs['height'],
                ))
            elif has_bottom: 
                self.style.update(dict(
                    left=x,
                    bottom=y, #-self._computed_style['height'],
                ))
            else:
                print "c"
     

    def on_window_resize(self, drag_corner, x, y):
        if drag_corner == 0:
            ldx = self._computed_style['left']-x
            ldy = self._computed_style['bottom']-y
            self.style.update(dict(
                left=x,
                bottom=y,
                width=self._computed_style['width']+ldx,
                height=self._computed_style['height']+ldy,
            ))
        elif drag_corner == 1:
            ldy = self._computed_style['bottom']-y
            self.style.update(dict(
                width=x-self._computed_style['left'],
                bottom=y,
                height=self._computed_style['height']+ldy,
            ))
        elif drag_corner == 2:
            self.style.update(dict(
                width=x-self._computed_style['left'],
                height=y-self._computed_style['bottom'],
            ))
        elif drag_corner == 3:
            ldx = self._computed_style['left']-x
            self.style.update(dict(
                left=x,
                height=y-self._computed_style['bottom'],
                width=self._computed_style['width']+ldx,
            ))

    def on_mouse_press(self, x, y, b, m):
        self.bring_to_front()
        return True

    def on_mouse_release(self, x, y, b, m):
        return True
            
  
Window.register_event_type('on_window_start_move')
Window.register_event_type('on_window_move')
Window.register_event_type('on_window_start_resize')
Window.register_event_type('on_window_resize')


